package org.jumiz.telnet.play;

import java.util.Arrays;
import java.util.Random;

/**
 *
 */
public class GuessingNumbersPlus extends AbstractPlay{

    boolean end;

    String number;

    @Override
    public boolean isEnd() {
        return end;
    }

    @Override
    public String first(String common) {
        String message = "玩法介绍：\n我想到一个4位的数字，你需要根据我的提示猜出我想到的数字，\n" +
                "如果数字对了，则用B标识，如果数字对了且位置对了，则用A标识。\n" +
                "例如：我想到的数字是1234\n" +
                "你的回答是: 1562,\n" +
                "我的答复是: 1A1B\n" +
                "我想好了一个数字，你来猜猜吧！\n";
        number = String.valueOf(new Random().nextInt(1000) + 1000);
        return message;
    }

    @Override
    public String next(String common) {
        if (common.length() != 4) {
            return "是四位数字哦！\n";
        }
        if (number.equals(common)) {
            end = true;
            return "恭喜猜对了！\n";
        }
        int acount = 0;
        int bcount = 0;
        for (int i = 0; i < common.length(); i++) {
            if (number.charAt(i) == common.charAt(i)) {
                acount++;
            } else if (number.contains(String.valueOf(common.charAt(i)))){
                bcount++;
            }
        }
        return String.format("%sA%sB\n", acount, bcount);
    }

    @Override
    public String end(String common) {
        return "欢迎下次来玩！\n";
    }

    @Override
    public String name() {
        return "猜数字升级版";
    }

    @Override
    public AbstractPlay clone() {
        return new GuessingNumbersPlus();
    }
}
